﻿using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;

namespace System.Serialization;

/// <summary>
/// YAML 序列化/反序列化 扩展
/// </summary>
public static class YamlExtend {

    public static T? GetObject<T>(string? serString) where T:class {
        return YamlExtend.GetObject(serString, typeof(T)) as T;
    }

    public static object GetObject(string? serString, Type type) {
        if (string.IsNullOrWhiteSpace(serString)) {
            return default;
        }

        StringReader input = new StringReader(serString);
        YamlStream tempYamlStream = new YamlStream();
        tempYamlStream.Load(input);
        return new DeserializerBuilder().IgnoreUnmatchedProperties().Build().Deserialize(serString,type);
    }

    public static string GetString(this object tObject) {
        return new SerializerBuilder().Build().Serialize(tObject);
    }

}
